package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.service.ILoginService;
import cn.wolfcode.rbac.web.annotation.RequiredPermission;
import cn.wolfcode.rbac.web.vo.LoginInfoVo;
import cn.wolfcode.rbac.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
//@CrossOrigin(allowCredentials = "true")  //局部注解，解决跨域的问题,在每个Controller层的类或方法上贴注解
@RequestMapping("/api")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @GetMapping("code")
    public R code() {
        Map<String, String> generator = loginService.generator();
        return R.ok(generator);
    }

    /**
     * 登录
     *
     * @param loginInfoVo 封装uuid,username,password,code
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody @Validated LoginInfoVo loginInfoVo) {
        //登录逻辑
        Employee loginEmployee = loginService.login(loginInfoVo);
        //响应数据
        return R.ok("登录成功", loginEmployee);
    }

    @DeleteMapping("logout")
    public R logout(@RequestHeader("userId") Long userId) {
        loginService.logout(userId);
        return R.ok("已退出登录");
    }
}
